package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.GeneratedFeed;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;

public class GeneratedForm extends Composite {
    private static final String ATOM_URL = GWT.getHostPageBaseURL() + "rscrssfeeds/atom?channel=";
    private GeneratedPanel parent;

    private FlexTable form = new FlexTable();

    private TextBox authorTB;
    private TextBox titleTb;
    private DateBox dateBox;

    private GeneratedFeed feed = new GeneratedFeed();

    public GeneratedForm(GeneratedPanel parent) {
	this.parent = parent;
	buildContent();
	initWidget(this.form);
    }

    public GeneratedFeed getFeed() {
	return this.feed;
    }

    public void refresh(GeneratedFeed feed) {
	this.feed = feed;
	buildContent();
    }

    private void buildContent() {
	this.form.clear();
	this.form.addStyleName("form");
	this.form.addStyleName("border");
	Label lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_TITLE, lb));
	this.form.setWidget(1, 0, lb);
	this.titleTb = new TextBox();
	this.titleTb.addStyleName("forminput");
	this.form.setWidget(1, 1, this.titleTb);
	this.titleTb.setValue(this.feed.getTitle());

	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_AUTHOR, lb));
	this.form.setWidget(2, 0, lb);
	this.authorTB = new TextBox();
	this.authorTB.addStyleName("forminput");
	this.form.setWidget(2, 1, this.authorTB);
	this.authorTB.setValue(this.feed.getAuthor());

	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_DATE, lb));
	this.form.setWidget(3, 0, lb);
	this.dateBox = new DateBox();
	this.dateBox.setFormat(new DateBox.DefaultFormat(Feeds.getDateFormat()));
	this.dateBox.addStyleName("forminput");
	this.form.setWidget(3, 1, this.dateBox);
	this.dateBox.setValue(this.feed.getPubDate());

	lb = new Label();
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_CHANNEL_URL, lb));
	this.form.setWidget(4, 0, lb);
	this.form.getFlexCellFormatter().setColSpan(4, 0, 2);

	TextBox urlTb = new TextBox();
	urlTb.setReadOnly(true);
	urlTb.setWidth("100%");
	Long fid = this.feed.getId();
	if (fid == null || fid < 0) {
	    urlTb.setText("N/A");
	} else {
	    urlTb.setText(ATOM_URL + fid);
	}
	this.form.setWidget(5, 0, urlTb);
	this.form.getFlexCellFormatter().setColSpan(5, 0, 2);

	HorizontalPanel buttonPanel = new HorizontalPanel();
	buttonPanel.addStyleName("buttonpanel");
	this.form.setWidget(6, 0, buttonPanel);
	this.form.getFlexCellFormatter().setColSpan(6, 0, 2);

	Button save = new Button();
	save.setText(Dictionary.getEntry(Dictionary.BUTTON_EDIT_PROFILE, save));
	buttonPanel.add(save);

	save.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		if (validate()) {
		    GeneratedForm.this.parent.save(GeneratedForm.this.feed);
		}
	    }
	});

	Button newBt = new Button();
	newBt.setText(Dictionary.getEntry(Dictionary.BUTTON_NEW_CHANNEL, save));
	buttonPanel.add(newBt);
	newBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		refresh(new GeneratedFeed());
	    }
	});

	Button delBt = new Button();
	delBt.setText(Dictionary.getEntry(Dictionary.BUTTON_DELETE, save));
	delBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		Feeds.getService().deleteGeneratedFeed(GeneratedForm.this.feed.getId(), new AsyncCallback<String>() {

		    @Override
		    public void onFailure(Throwable caught) {
			Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_COMM_ERROR, null));
		    }

		    @Override
		    public void onSuccess(String result) {
			if (result != null) {
			    Window.alert(Dictionary.getEntry(result, null));
			} else {
			    refresh(new GeneratedFeed());
			    GeneratedForm.this.parent.refreshForm(new GeneratedFeed());
			    GeneratedForm.this.parent.refreshFeedsTable();
			}
		    }
		});
	    }
	});
	buttonPanel.add(delBt);

    }

    private boolean validate() {
	this.feed.setAuthor(this.authorTB.getValue());
	this.feed.setPubDate(this.dateBox.getValue());
	this.feed.setTitle(this.titleTb.getValue());
	StringBuilder error = new StringBuilder();
	if (this.authorTB.getValue() == null || this.authorTB.getValue().length() < 1) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_AUTHOR_REQUIRED, null)).append("\r\n");
	}
	if (this.titleTb.getValue() == null || this.titleTb.getValue().length() < 1) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_TILE_REQUIRED, null)).append("\r\n");
	}
	if (this.dateBox.getValue() == null) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_PUBDATE_REQUIRED, null)).append("\r\n");
	}
	if (error.length() > 0) {
	    Window.alert(error.toString());
	    return false;
	}
	return true;
    }
}
